﻿using System.Net.Mail;
using System.Net;
using System.Text;
using WebLog;
namespace  Logic
{
    internal sealed class Mail
    {
        public static void SendMail(string to, string body, string subject, string userMail, string password, string mailServer, string attachments)
        {
            using (MailMessage mensaje = new MailMessage(userMail, to, subject, body))
            using (SmtpClient smtpCliente = new SmtpClient())
            {
                try
                {
                    
                    mensaje.Subject = subject;
                    mensaje.SubjectEncoding = Encoding.UTF8;
                    mensaje.Body = body;
                    mensaje.BodyEncoding = Encoding.UTF8;
                    mensaje.IsBodyHtml = false;
                    if (attachments != null)
                    {
                        mensaje.Attachments.Add(new Attachment(attachments));
                    }

                    smtpCliente.EnableSsl = true;
                    smtpCliente.DeliveryMethod = SmtpDeliveryMethod.Network;
                    smtpCliente.Credentials = new NetworkCredential(userMail, password);
                    smtpCliente.Host = mailServer;
                    smtpCliente.Send(mensaje);

                }
                catch (SmtpException ex)
                {
                    Logger logger = Logger.GetInstance;
                    logger.WriteLog(ex);
                }
            }
        }
    }
}
